package kocla.com.myapplication;

import android.app.Activity;
import android.database.SQLException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.j256.ormlite.android.apptools.OrmLiteBaseListActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;

import java.util.ArrayList;
import java.util.List;

import kocla.com.myapplication.bean.User;
import kocla.com.myapplication.utils.DatabaseHelper;

public class MainActivity extends OrmLiteBaseListActivity {
 /*   private ImageView imageView;
    private EditText et_phone, et_password;
    private Button button;
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    public static AsyncHttpClient ahc = null;
    private Bitmap bitmap;

    {
        ahc = new AsyncHttpClient();
    }*/


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        User u1 = new User("zhy", "2B青年");
        DatabaseHelper helper = DatabaseHelper.getHelper(this);
        try {
            helper.getUserDao().create(u1);

            u1 = new User("zhy2", "2B青年");
            helper.getUserDao().create(u1);
            u1 = new User("zhy3", "2B青年");
            helper.getUserDao().create(u1);
            u1 = new User("zhy4", "2B青年");
            helper.getUserDao().create(u1);
            u1 = new User("zhy5", "2B青年");
            helper.getUserDao().create(u1);
            u1 = new User("zhy6", "2B青年");
            helper.getUserDao().create(u1);
            testList();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }


     /*   imageView = (ImageView) findViewById(R.id.imageView);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_password = (EditText) findViewById(R.id.et_password);
        button = (Button) findViewById(R.id.button);*/
     /*   Glide.with(this).load("http://218.17.158.37:8147/zcstudy/shengChengYanZhengMa?shouJiHaoMa=" + et_phone.getText().toString()).placeholder(R.mipmap.ic_launcher).into(imageView);
        RequestParams params = new RequestParams();
        params.put("shouJiHaoMa", et_phone.getText().toString());*/
        /*String url = "http://218.17.158.37:8147/zcstudy/shengChengYanZhengMa";

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    SimpleClient.getHttpClient();
                  *//*  HttpEntity httpEntity = SimpleClient.doPost("http://218.17.158.37:8147/zcstudy/shengChengYanZhengMa", params);
                    byte[] data = EntityUtils.toByteArray(httpEntity);
                    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// 将图片转换成bitmap类型*//*
                    String url = "http://218.17.158.37:8147/zcstudy/shengChengYanZhengMa?shouJiHaoMa=18720180559";
                    bitmap = SimpleClient.getImageBitmap(url);
                    handler.sendEmptyMessage(1);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                *//*RequestParams params = new RequestParams();
                params.put("shouJiHaoMa", et_phone.getText().toString());
                params.put("yanZhengMa", et_password.getText().toString());

                String url = "http://218.17.158.37:8147/zcstudy/heDuiYanZhengMa";
//                Log.i("MainActivity", url + "?" + params.toString());
                ahc.post(url, params, new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, org.apache.http.Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
//                        Log.i("MainActivity", response.toString());
                    }

                    @Override
                    public void onFailure(int statusCode, org.apache.http.Header[] headers, Throwable throwable, JSONObject errorResponse) {
                        super.onFailure(statusCode, headers, throwable, errorResponse);
//                        Log.i("MainActivity", errorResponse.toString());
                    }
                });*//*
                final String url = "http://218.17.158.37:8147/zcstudy/heDuiYanZhengMa?shouJiHaoMa=" + et_phone.getText().toString() + "&yanZhengMa=" + et_password.getText().toString();

                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        SimpleClient.getURLResponse(url);
                    }
                }.start();


            }
        });*/





    }


    public void testList() {
        DatabaseHelper helper = DatabaseHelper.getHelper(this);
        try {
            User u1 = new User("zhy-android", "2B青年");
            u1.setId(2);
            List<User> users = null;
            try {
                users = helper.getUserDao().queryForAll();
            } catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
            Log.i("MainActivity", users.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /*Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    };*/
}
